/**
 *====================================================
 * 文件名称: CalculatePropertyExpression.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2016年1月20日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.support;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.CriteriaQuery;
import org.hibernate.criterion.Criterion;
import org.hibernate.engine.spi.TypedValue;

/**
 * @ClassName: CalculatePropertyExpression
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2016年1月20日 上午11:33:43
 */
public class CalculatePropertyExpression implements Criterion {

	private static final long serialVersionUID = 1L;

	private static final TypedValue[] NO_TYPED_VALUES = new TypedValue[0];

	private final String propertyName;

	private final String otherPropertyName;

	private final String calculatePropertyName;

	private final String op;

	public CalculatePropertyExpression(String propertyName, String otherPropertyName, String calculatePropertyName, String op) {
		this.propertyName = propertyName;
		this.otherPropertyName = otherPropertyName;
		this.calculatePropertyName = calculatePropertyName;
		this.op = op;
	}

	@Override
	public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
		final String[] lhsColumns = criteriaQuery.findColumns(propertyName, criteria);
		final String[] rhsColumns = criteriaQuery.findColumns(otherPropertyName, criteria);
		final String[] rhsCalculateColumns = criteriaQuery.findColumns(calculatePropertyName, criteria);
		return '(' + lhsColumns[0] + this.op + rhsColumns[0] + " + " + rhsCalculateColumns[0] + ')';
	}

	@Override
	public TypedValue[] getTypedValues(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
		return NO_TYPED_VALUES;
	}

	@Override
	public String toString() {
		return propertyName + this.op + this.otherPropertyName + " + " + this.calculatePropertyName;
	}

}
